/*
 * claplan_frame.cpp
 *
 *  Created on: 31 Oct 2013
 *      Author: Tom Vercauteren
 */

#include "claplan_view.h"
#include <wx/filedlg.h>

#include <string>
#include <iostream>
#include "string_utilities.h"
#include "debug.h"

using namespace std;

ClaplanView::ClaplanView(const wxString& title) :
		wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(800, 600)), controller(
				0), menuBar(0), menuFile(0), menuItemNewFile(0), menuItemOpenFile(
				0), menuItemCloseFile(0), menuItemExit(0), menuHelp(0), menuItemAbout(
				0), cltree(0)
{
	createMenus();
	createPanel();
	Centre();
}

ClaplanView::~ClaplanView()
{
}

void ClaplanView::claplanDataChanged()
{
	here;
	if (cltree != 0) cltree->claplanDataChanged();
}

void ClaplanView::createMenus()
{
	menuBar = new wxMenuBar();

	// File menu
	menuFile = new wxMenu();
	menuItemNewFile = new wxMenuItem(menuFile, wxID_NEW, wxT("&New"),
			wxT("Create a new claplan file"));
	menuFile->Append(menuItemNewFile);
	menuItemOpenFile = new wxMenuItem(menuFile, wxID_OPEN, wxT("&Open"),
			wxT("Open an existing claplan file"));
	menuFile->Append(menuItemOpenFile);
	menuItemSaveFile = new wxMenuItem(menuFile, wxID_SAVE, wxT("&Save"),
			wxT("Save the claplan"));
	menuFile->Append(menuItemSaveFile);
	menuItemCloseFile = new wxMenuItem(menuFile, wxID_CLOSE, wxT("&Close"),
			wxT("Close a claplan file"));
	menuFile->Append(menuItemCloseFile);
	menuFile->AppendSeparator();
	menuItemExit = new wxMenuItem(menuFile, wxID_EXIT, wxT("E&xit"),
			wxT("Exit program"));
	menuFile->Append(menuItemExit);

	// Help menu
	menuHelp = new wxMenu();
	menuItemAbout = new wxMenuItem(menuHelp, wxID_ANY, wxT("About"),
			wxT("About this program"));
	menuHelp->Append(menuItemAbout);

	menuBar->Append(menuFile, wxT("&File"));
	menuBar->Append(menuHelp, wxT("&Help"));

	// Connect
	Connect(wxID_NEW, wxEVT_COMMAND_TOOL_CLICKED,
			wxCommandEventHandler(ClaplanView::OnNewFile));
	Connect(wxID_OPEN, wxEVT_COMMAND_TOOL_CLICKED,
			wxCommandEventHandler(ClaplanView::OnOpen));
	Connect(wxID_EXIT, wxEVT_COMMAND_TOOL_CLICKED,
			wxCommandEventHandler(ClaplanView::OnExit));

	SetMenuBar(menuBar);

	CreateStatusBar(1);
	SetStatusText(wxT("Created all menu's"), 0);
}

void ClaplanView::createTree(wxWindow *parent)
{
	if (cltree == 0)
	{
		cltree = new ClaplanTree(parent);
		cltree->AddRoot(wxT("Claplans"));
	}
}

void ClaplanView::createPanel()
{
	splitw = new wxSplitterWindow(this, wxID_ANY);
	splitw->SetSashGravity(0.0);

	leftPanel = new wxPanel(splitw, wxID_ANY);
	wxFlexGridSizer *leftSizer = new wxFlexGridSizer(1, 1, 5, 5);
	createTree(leftPanel);
	leftSizer->Add(cltree, 1, wxEXPAND | wxALL);
	leftSizer->AddGrowableCol(0, 1);
	leftSizer->AddGrowableRow(0, 1);
	leftPanel->SetSizer(leftSizer);

	rightPanel = new wxPanel(splitw, wxID_ANY);
	new wxStaticText(rightPanel, wxID_ANY, wxT("Temp panel replacement"));

	splitw->SplitVertically(leftPanel, rightPanel, 200);
}

void ClaplanView::setController(ClaplanController *controller)
{
	this->controller = controller;
}

void ClaplanView::removeController()
{
	this->controller = 0;
}

void ClaplanView::OnNewFile(wxCommandEvent& WXUNUSED(event))
{
	wxFileDialog *newDialog = new wxFileDialog(this);
	newDialog->SetTitle(wxT("Create a new claplan"));
	if (newDialog->ShowModal() == wxID_OK)
	{
		string path = string(newDialog->GetPath().mbc_str());
		if (controller != 0)
		{
			int rv = controller->newClaplanFile(path);
			if (rv != 0)
			{
				string msg = string("Something went wrong while creating ")
						+ "the claplan file : " + path + ". Return value was "
						+ int2str(rv);
				wxMessageBox(wxString(msg.c_str(), wxConvUTF8),
						wxT("Error creating claplan file"),
						wxOK | wxICON_ERROR);
				SetStatusText(wxT("Claplan file not created."), 0);
			}
			else SetStatusText(wxT("Claplan file created."), 0);
		}
		else
		{
			string msg = "Unable to create : " + path
					+ " because no controller is defined";
			SetStatusText(wxString(msg.c_str(), wxConvUTF8), 0);
		}
	}
}

void ClaplanView::OnClose(wxCommandEvent& WXUNUSED(event))
{
	wxTreeItemId selection = cltree->GetSelection();
	if (selection.IsOk())
	{
		wxString selName = cltree->GetItemText(selection);
	}
}

void ClaplanView::OnExit(wxCommandEvent& WXUNUSED(event))
{
	this->Close(true);
}

void ClaplanView::OnOpen(wxCommandEvent& WXUNUSED(event))
{
	wxFileDialog* openDialog = new wxFileDialog(this);
	openDialog->SetTitle(wxT("Open an existing claplan"));
	if (openDialog->ShowModal() == wxID_OK)
	{
		string path = string(openDialog->GetPath().mbc_str());
		if (controller != 0)
		{
			int rv = controller->openClaplanFile(path);
			if (rv != 0)
			{
				string msg = string("Something went wrong while opening ")
						+ "the claplan file : " + path + ". Return value was "
						+ int2str(rv);
				wxMessageBox(wxString(msg.c_str(), wxConvUTF8),
						wxT("Error reading claplan file"),
						wxOK | wxICON_ERROR);
				SetStatusText(wxT("Claplan file not opened."), 0);
			}
			else
			{
				SetStatusText(wxT("Claplan file opened."), 0);
			}
		}
		else
		{
			string msg = "Unable to open : " + path
					+ " because no controller is defined";
			SetStatusText(wxString(msg.c_str(), wxConvUTF8), 0);
		}
	}
}

void ClaplanView::OnSelectionChange(wxTreeEvent &event)
{
	if (event.GetEventObject() == cltree)
	{
		if (cltree != 0 && controller != 0)
		{
//			cout << "ClaplanView::OnSelectionChange : "
//					<< "Is claplan file selected? : "
//					<< cltree->isClaplanFileSelected() << endl;

			wxTreeItemId sel = cltree->GetSelection();
			if (sel.IsOk())
			{
				string scl="";
				string ssec="";
				if (cltree->isClaplanFileSelected() == true)
				{
					scl = string(cltree->GetItemText(sel).mbc_str());
				}
				else if (cltree->GetRootItem() != sel)
				{
					ssec = string(cltree->GetItemText(sel).mbc_str());
					wxTreeItemId parent = cltree->GetItemParent(sel);
					if (parent.IsOk()) scl = string(cltree->GetItemText(parent).mbc_str());
				}
				controller->selectionTreeChanged(scl, ssec);
			}

		}
	}
}

void ClaplanView::connectTreeEvents()
{
	Connect(wxEVT_COMMAND_TREE_SEL_CHANGED,
			wxTreeEventHandler(ClaplanView::OnSelectionChange));
}

void ClaplanView::disconnectTreeEvents()
{
	Disconnect(wxEVT_COMMAND_TREE_SEL_CHANGED,
			wxTreeEventHandler(ClaplanView::OnSelectionChange));
}

int ClaplanView::addClaplan(const std::string &name)
{
	return (cltree != 0) ? cltree->addClaplan(name) : -1;
}

int ClaplanView::addClaplanSection(const std::string &name,
		const std::string &section)
{
	return (cltree != 0) ? cltree->addClaplanSection(name, section) : -1;
}
int ClaplanView::removeClaplan(const std::string &name)
{
	return (cltree != 0) ? cltree->removeClaplan(name) : -1;
}

int ClaplanView::removeClaplanSection(const std::string &name,
		const std::string &section)
{
	return (cltree != 0) ? cltree->removeClaplanSection(name, section) : -1;
}

void ClaplanView::setTitle(const std::string &name)
{
	SetTitle(wxString(name.c_str(),wxConvUTF8));
}
